package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private SetmealMapper setmealMapper;
	
	/**
	 * 今日数据
	 *
	 * @return
	 */
	@Override
	public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
		//查询总订单数
		Integer totalOrderCount = orderMapper.countNumber(null, begin, end);
		//营业额
		Double turnover = orderMapper.sumAmount(Orders.COMPLETED, begin, end);
		turnover = turnover == null ? 0.0 : turnover;
		//有效订单数
		Integer validOrderCount = orderMapper.countNumber(Orders.COMPLETED, begin, end);
		Double unitPrice = 0.0;
		Double orderCompletionRate = 0.0;
		if (totalOrderCount != 0 && validOrderCount != 0) {
			//订单完成率
			orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
			//平均客单价
			unitPrice = turnover / validOrderCount;
		}
		//新增用户数
		Integer newUsers = userMapper.countNumber(begin, end);
		
		return BusinessDataVO.builder().turnover(turnover).validOrderCount(validOrderCount).orderCompletionRate(orderCompletionRate).unitPrice(unitPrice).newUsers(newUsers).build();
	}
	
	/**
	 * 套餐总览
	 *
	 * @return
	 */
	@Override
	public SetmealOverViewVO overviewSetmeals() {
		return SetmealOverViewVO.builder().sold(setmealMapper.countNumber(StatusConstant.ENABLE)).discontinued(setmealMapper.countNumber(StatusConstant.DISABLE)).build();
	}
	
	/**
	 * 菜品总览
	 *
	 * @return
	 */
	@Override
	public DishOverViewVO overviewDishes() {
		return DishOverViewVO.builder().sold(dishMapper.countNumber(StatusConstant.ENABLE)).discontinued(dishMapper.countNumber(StatusConstant.DISABLE)).build();
	}
	
	@Override
	public OrderOverViewVO overviewOrders() {
		LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);
		//待接单
		Integer waitingOrders = orderMapper.countNumber(Orders.TO_BE_CONFIRMED, begin, null);
		//待派送
		Integer deliveredOrders = orderMapper.countNumber(Orders.CONFIRMED, begin, null);
		//已完成
		Integer completedOrders = orderMapper.countNumber(Orders.COMPLETED, begin, null);
		//已取消
		Integer cancelledOrders = orderMapper.countNumber(Orders.CANCELLED, begin, null);
		//全部订单
		Integer allOrders = orderMapper.countNumber(null, begin, null);
		
		return OrderOverViewVO.builder().waitingOrders(waitingOrders).deliveredOrders(deliveredOrders).completedOrders(completedOrders).cancelledOrders(cancelledOrders).allOrders(allOrders).build();
	}
}
